<template>
  <div class="app-container">
    <div class="filter-more">
      <div class="from_header">
        <el-form ref="form" label-width="120px" inline>
          <el-form-item label="供应商名称：">
            <el-input
              v-model.trim="query.manufacturer"
              clearable
              size="small"
              placeholder="请输入供应商名称"
              @clear="selected"
            />
          </el-form-item>
          <el-form-item label="供应商代码：">
            <el-input
              v-model.trim="query.manufacturerCode"
              clearable
              size="small"
              placeholder="请输入供应商代码"
              @clear="selected"
            />
          </el-form-item>

          <el-form-item label="">
            <el-button
              type="primary"
              size="small"
              @click="selected"
            >查询</el-button
            >
            <el-button
              type="default"
              size="small"
              @click="restList"
            >重置</el-button
            >
          </el-form-item>
        </el-form>

        <div class="establish">
          <el-button
            type="primary"
            size="small"
            @click="add"
          >新增</el-button
          >
        </div>
      </div>
    </div>

    <el-table
      :data="list"
      :header-row-style="{
        'background-color': '#E6E6E6',
        'font-weight': 'bold',
        color: '#333',
      }"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      height="650"
      align="center"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="manufacturer"
        label="供应商名称"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="manufacturerCode"
        label="供应商代码"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="bankName"
        label="开户行名称"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="bankAccount"
        label="开户行账号"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="taxNum"
        label="税号"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="address"
        label="地址"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="phone"
        label="电话"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="contactPerson"
        label="联系人"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="faxNum"
        label="传真"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="createDt"
        label="创建日期"
      />
      <el-table-column fixed="right" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="() => deleteRow(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="query.page"
      :total="total"
      :page-size="query.limit"
      style="padding: 30px 0; text-align: end"
      layout="total, prev, pager, next,jumper"
      @current-change="fetchData"
    />

    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogFormValue"
      @close="closeDialog"
    >
      <el-form
        ref="add"
        :model="addForm"
        :rules="validateAddForm"
        label-width="120px"
      >
        <el-form-item label="供应商名称：" prop="manufacturer">
          <el-col :span="8">
            <el-input
              v-model="addForm.manufacturer"
              size="small"
              placeholder="请输入供应商名称"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="供应商代码：" prop="manufacturerCode">
          <el-col :span="8">
            <el-input
              v-model="addForm.manufacturerCode"
              size="small"
              placeholder="请输入供应商代码"
            />
          </el-col>
        </el-form-item>

        <el-form-item label="开户行名称：" prop="bankName">
          <el-col :span="8">
            <el-input
              v-model="addForm.bankName"
              size="small"
              placeholder="请输入开户行名称"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="开户行账号：" prop="bankAccount">
          <el-col :span="8">
            <el-input
              v-model="addForm.bankAccount"
              size="small"
              placeholder="请输入开户行账号"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="税号：" prop="taxNum">
          <el-col :span="8">
            <el-input
              v-model="addForm.taxNum"
              size="small"
              placeholder="请输入税号"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="地址：" prop="address">
          <el-col :span="8">
            <el-input
              v-model="addForm.address"
              size="small"
              placeholder="请输入地址"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="电话：" prop="phone">
          <el-col :span="8">
            <el-input
              v-model="addForm.phone"
              size="small"
              placeholder="请输入电话"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="联系人：" prop="contactPerson">
          <el-col :span="8">
            <el-input
              v-model="addForm.contactPerson"
              size="small"
              placeholder="请输入联系人"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="传真：" prop="faxNum">
          <el-col :span="8">
            <el-input
              v-model="addForm.faxNum"
              size="small"
              placeholder="请输入传真"
            />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="restData()">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// { listManufacturer, updateManufacturer }
import user from '@/api/acl/user.js'

export default {
  data() {
    return {
      query: {
        manufacturer: '',
        manufacturerCode: '',
        page: 1,
        limit: 10
      },
      total: 1,
      list: [],
      loading: false,
      addForm: {
        manufacturer: '',
        manufacturerCode: '',
        bankName: '',
        bankAccount: '',
        taxNum: '',
        address: '',
        phone: '',
        contactPerson: '',
        faxNum: ''
      },
      validateAddForm: {
        manufacturer: [
          {
            required: true,
            trigger: 'change',
            message: '请输入供应商名称'
          }
        ],
        manufacturerCode: [
          {
            required: true,
            trigger: 'change',
            message: '请输入供应商代码'
          }
        ]
      },
      dialogFormVisible: false,
      dialogFormValue: '新增供应商'
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    restList() {
      this.query.page = 1
      this.query.manufacturer = ''
      this.query.manufacturerCode = ''
      this.getList()
    },
    restData() {
      this.dialogFormVisible = false
      //   this.dialogFormValue = "";
    },
    selected() {
      this.query.page = 1
      this.getList()
    },
    fetchData(val = 1) {
      this.query.page = val
      this.getList()
    },
    dateStr(timeStr) {
      const d = new Date(timeStr)

      return `${d.getFullYear()}-${
        d.getMonth() + 1
      }-${d.getDate()} ${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`
    },
    getList() {
      console.log('this.query: ', this.query)
      user.listManufacturer(this.query).then((response) => {
        this.total = response.total
        this.list = response.list.map((item) => {
          item.createDt = this.dateStr(item.createDt)

          return item
        })
      })
    },
    deleteRow(val) {
      if (!val.id) return false
      this.$confirm('是否删除该供应商吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          user.updateManufacturer({ id: val.id, deleteFlag: 1 }).then(
            (res) => {
              this.$message({
                message: '删除成功',
                duration: 1000
              })
              this.getList()
            }
          )
        })
        .catch(() => {})
    },
    add() {
      this.dialogFormVisible = true
    },
    confirmAdd() {
      this.$refs.add.validate((valid) => {
        if (valid) {
          user.insertManufacturer(this.addForm).then((res) => {
            this.getList()
            this.$message({
              message: '新增成功',
              duration: 1000
            })
            this.dialogFormVisible = false
          })
        }
      })
    },
    closeDialog() {
      this.$refs.add.clearValidate()
      this.$refs.add.resetFields()
    }
  }
}
</script>

<style lang="scss">
.establish {
	text-align: right;
}
</style>
